package com.dgut.smart_house.controller;

import com.dgut.smart_house.domain.KeyCard;
import com.dgut.smart_house.enums.ResultCode;
import com.dgut.smart_house.service.KeyCardService;
import com.dgut.smart_house.service.impl.AsyncService;
import com.dgut.smart_house.util.RestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/data/keyCard/api")
public class KeyCardApiController {

    @Autowired
    private KeyCardService keyCardService;
    @Autowired
    private AsyncService asyncService;

    /**
     * 查询该用户所有卡号成员
     *
     * @param session
     * @return
     */
    @RequestMapping("/findAllCard")
    public RestResult findAllCard(HttpSession session) {
        int fromUser = (int) session.getAttribute("IdFromUserTable");
        List<KeyCard> keyCards = keyCardService.findAllCard(fromUser);
        if (keyCards == null && keyCards.size() == 0)
            return new RestResult(ResultCode.FAIL.getCode(), "没有门成员数据", null);
        return new RestResult(ResultCode.SUCCESS, keyCards);
    }

    ;

    /**
     * 创建一个卡号成员
     *
     * @param session
     * @param keyCard
     * @return
     */
    @RequestMapping("/createCardMaster")
    public RestResult createCardMaster(HttpSession session, KeyCard keyCard) {
        int fromUser = (int) session.getAttribute("IdFromUserTable");
        keyCard.setFromUser(fromUser);
        keyCardService.createCardMaster(keyCard);
        log.info("用户-{}-尝试传建了一个卡号-{}-备注信息-{}", fromUser, keyCard.getCardId(), keyCard.getCardDesc());
        return new RestResult(ResultCode.SUCCESS.getCode(), "异步任务处理中，10秒后有结果！", null);
    }

    ;

    /**
     * 删除卡号
     *
     * @param keyId
     * @return
     */
    @DeleteMapping("/deleteCardMaster/{keyId}")
    public RestResult deleteCardMaster(@PathVariable("keyId") Integer keyId) {
        KeyCard keyCard = keyCardService.findCardByKey(keyId);
        keyCardService.deleteCardMaster(keyId);
        log.info("用户-{}-删除了一个卡号-{}-备注信息-{}", keyCard.getFromUser(), keyCard.getCardId(), keyCard.getCardDesc());
        return new RestResult(ResultCode.SUCCESS.getCode(), "删除成功", null);
    }

    ;

    /**
     * 更新卡号成员名
     *
     * @param keyId
     * @param cardDesc
     * @return
     */
    @PutMapping("/updateCardMaster/{keyId}")
    public RestResult updateCardMaster(@PathVariable(value = "keyId", required = true) int keyId,
                                       @RequestParam(value = "cardDesc", required = true) String cardDesc) {
        keyCardService.updateCardMaster(cardDesc, keyId);
        KeyCard keyCard = keyCardService.findCardByKey(keyId);
        log.info("用户-{}-更新了卡号信息-{}-备注信息-{}", keyCard.getFromUser(), keyCard.getCardId(), keyCard.getCardDesc());

        return new RestResult(ResultCode.SUCCESS.getCode(), "修改成功", null);
    }

    ;


    @RequestMapping("/createData")
    public RestResult createData(KeyCard keyCard) {
        keyCardService.createData(keyCard);
        return new RestResult(ResultCode.SUCCESS.getCode(), "创建成功", null);
    }

    ;

    /**
     * 查询所有门禁日志
     *
     * @param session
     * @return
     */
    @RequestMapping("/findAllCardLog")
    public RestResult findAllCardLog(HttpSession session,
                                     @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                     @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        int fromUser = (int) session.getAttribute("IdFromUserTable");
        return new RestResult(ResultCode.SUCCESS, keyCardService.findAllCardLog(fromUser, pageNo, pageSize));
    }
}
